package com.iemes.controller.forming;


import java.util.ArrayList;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ChangeShiftsFormMap;
import com.iemes.entity.CheckContentFormMap;
import com.iemes.entity.MoldingInspectionFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.forming.ChangeShiftsMapper;
import com.iemes.mapper.forming.CheckContentMapper;
import com.iemes.mapper.forming.MoldingInspectionMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
@Controller
@RequestMapping("handover")
public class ChangeShiftsController extends BaseController{
	@Inject
	private ChangeShiftsMapper shiftsMapper;
	@Inject
	private CheckContentMapper contentMapper;
	@Inject
	private MoldingInspectionMapper inspectionMapper;
	
	@RequestMapping(value = "inspect", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		//如果不是主管
		if (userFormMap.get("site").equals("0")) {
			List<Map<String, Object>> map = new ArrayList<>();
			List<MoldingInspectionFormMap> inspection = inspectionMapper.findByAttribute("Status", "1", MoldingInspectionFormMap.class);	
			CheckContentFormMap checkContentFormMap = getFormMap(CheckContentFormMap.class);
			for (int i = 0; i < inspection.size(); i++) {
				checkContentFormMap.put("where", "where Status='1' and InspectionId="+inspection.get(i).get("id"));
				List<CheckContentFormMap> checkContent=contentMapper.findByWhere(checkContentFormMap);
				for (int j = 0; j < checkContent.size(); j++) {
					map.add(checkContent.get(j));
				}
			}
			//点检项目
			model.addAttribute("inspection", inspection);
			//点检内容
			model.addAttribute("checkContent", map);
			return Common.BACKGROUND_PATH + "/forming/changeshifts/inspect_confirm";
		}
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/forming/changeshifts/inspect_list";	
	}
	@ResponseBody
	@RequestMapping("inspect_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		ChangeShiftsFormMap changeShiftsFormMap = getFormMap(ChangeShiftsFormMap.class);
		List<Map<String, Object>> findByPage = shiftsMapper.getChangeShifts(changeShiftsFormMap);
		try {
			LuogicPaging(pageNow, pageSize, findByPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageView;
	}
	@RequestMapping("inspect_add")
	public String addUI(Model model)throws Exception {
		return Common.BACKGROUND_PATH + "/forming/changeshifts/inspect_add";
	}

	@ResponseBody
	@RequestMapping("inspect_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "成型管理", methods = "交接班检查-新增点检项目内容")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		//先新增点检项目
		MoldingInspectionFormMap moldingInspectionFormMap =getFormMap(MoldingInspectionFormMap.class);
		moldingInspectionFormMap.put("Date", DateUtils.getStringDateTime());
		moldingInspectionFormMap.put("Status", 1);
		inspectionMapper.addEntity(moldingInspectionFormMap);
		String[] check_Content = getParaValues("Check_Content");
		CheckContentFormMap checkContentFormMap = new CheckContentFormMap();
		for (int i = 0; i < check_Content.length; i++) {
			checkContentFormMap.put("Check_Content", check_Content[i]);
			checkContentFormMap.put("Date", DateUtils.getStringDateTime());
			checkContentFormMap.put("InspectionId", moldingInspectionFormMap.get("id"));
			checkContentFormMap.put("Status", 1);
			contentMapper.addEntity(checkContentFormMap);
			checkContentFormMap.clear();
		}
		return "success";
	}
	
	@RequestMapping("inspect_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		//1.先查出点检内容下的点检项目id
		ChangeShiftsFormMap findbyFrist = shiftsMapper.findbyFrist("id", id, ChangeShiftsFormMap.class);
		
		model.addAttribute("changeShifts", findbyFrist);
		return Common.BACKGROUND_PATH + "/forming/changeshifts/inspect_edit";
	}

	@ResponseBody
	@RequestMapping(value = "inspect_editEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-修改交接班检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		ChangeShiftsFormMap changeShiftsFormMap = getFormMap(ChangeShiftsFormMap.class);
		changeShiftsFormMap.put("Date", DateUtils.getStringDateTime());
		shiftsMapper.editEntity(changeShiftsFormMap);		
		return "success";
	}
	@ResponseBody
	@RequestMapping(value = "inspect_deleteEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-删除交接班检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		ChangeShiftsFormMap changeShiftsFormMap = new ChangeShiftsFormMap();
		for (int i = 0; i < str.length; i++) {
			changeShiftsFormMap.put("Status", 2);
			changeShiftsFormMap.put("id", str[i]);
			changeShiftsFormMap.put("Date", DateUtils.getStringDateTime());
			contentMapper.editEntity(changeShiftsFormMap);
		}
		return "success";
	}
	
	
	//编辑项目
	@RequestMapping("inspect_editItem")
	public String editItem(Model model)throws Exception {
		MoldingInspectionFormMap moldingInspectionFormMap = new MoldingInspectionFormMap();
		moldingInspectionFormMap.put("where", "where Status=1 and Department=1");
		List<MoldingInspectionFormMap> findByWhere = inspectionMapper.findByWhere(moldingInspectionFormMap);
		model.addAttribute("inspection", findByWhere);
		return Common.BACKGROUND_PATH + "/forming/changeshifts/inspect_editItem";
	}
	
	@ResponseBody
	@RequestMapping(value = "inspect_searchEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-查询点检内容")
	// 凡需要处理业务逻辑的.都需要记录操作日志inspect_itemDeleteEntity
	public List<CheckContentFormMap> searchEntity() throws Exception {
		String id = getPara("id");
		CheckContentFormMap checkContentFormMap = new CheckContentFormMap();
		checkContentFormMap.put("where", "where Status='1' and InspectionId="+id);
		return contentMapper.findByWhere(checkContentFormMap);
	}
	
	@ResponseBody
	@RequestMapping(value = "inspect_itemDeleteEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-查询点检内容")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String itemDelete() throws Exception {
		String id = getPara("id");
		//先删除点检内容
		List<CheckContentFormMap> checkContentFormMap = contentMapper.findByAttribute("InspectionId", id, CheckContentFormMap.class);
		for (int i = 0; i < checkContentFormMap.size(); i++) {
			checkContentFormMap.get(i).put("Status", 2);
			checkContentFormMap.get(i).put("Date", DateUtils.getStringDateTime());
			contentMapper.editEntity(checkContentFormMap.get(i));
		}
		//后删除点检项目
		MoldingInspectionFormMap moldingInspectionFormMap = new MoldingInspectionFormMap();
		moldingInspectionFormMap.put("id", id);
		moldingInspectionFormMap.put("Date", DateUtils.getStringDateTime());
		moldingInspectionFormMap.put("Status", 2);
		inspectionMapper.editEntity(moldingInspectionFormMap);
		return "success" ;
	}
	
	@ResponseBody
	@RequestMapping(value = "inspect_editItemEntity")
	@SystemLog(module = "成型管理", methods = "交接班检查-修改点检内容")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editItem() throws Exception {
		//先把之前点检内容删除再增加新的
		MoldingInspectionFormMap moldingInspectionFormMap = getFormMap(MoldingInspectionFormMap.class);
		List<CheckContentFormMap> checkContentFormMap = contentMapper.findByAttribute("InspectionId", moldingInspectionFormMap.get("id").toString(), CheckContentFormMap.class);
		for (int i = 0; i < checkContentFormMap.size(); i++) {
			checkContentFormMap.get(i).put("Status", 2);
			checkContentFormMap.get(i).put("Date", DateUtils.getStringDateTime());
			contentMapper.editEntity(checkContentFormMap.get(i));
		}
		//新增加
		String[] check_Content = getParaValues("Check_Content");
		CheckContentFormMap checkContent = new CheckContentFormMap();
		for (int i = 0; i < check_Content.length; i++) {
			checkContent.put("Status", 1);
			checkContent.put("Check_Content", check_Content[i]);
			checkContent.put("InspectionId", moldingInspectionFormMap.get("id"));
			checkContent.put("Date",  DateUtils.getStringDateTime());
			contentMapper.addEntity(checkContent);
			checkContent.clear();
			
		}
		
		return "success" ;
	}
	
	@ResponseBody
	@RequestMapping(value = "inspect_confirmAdd" ,method=RequestMethod.POST)
	@SystemLog(module = "成型管理", methods = "交接班检查-确认点检项目内容")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String confirmEntity() throws Exception {
		ChangeShiftsFormMap changeShiftsFormMap = getFormMap(ChangeShiftsFormMap.class);
		String machine_Number = getPara("changeshifts.Machine_Number");
		String mold_Number = getPara("changeshifts.Mold_Number");
		String[] check_Content = getParaValues("changeshifts.Check_Content");
		String[] spotCheck = getParaValues("changeshifts.SpotCheck");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < check_Content.length; i++) {
			changeShiftsFormMap.put("Machine_Number", machine_Number);
			changeShiftsFormMap.put("Mold_Number", mold_Number);
			changeShiftsFormMap.put("CheckContent", check_Content[i]);
			changeShiftsFormMap.put("SpotCheck", spotCheck[i]);
			changeShiftsFormMap.put("Spot_Checker",userFormMap.get("userName"));
			changeShiftsFormMap.put("Date",DateUtils.getStringDateTime());
			changeShiftsFormMap.put("Status",1);
			shiftsMapper.addEntity(changeShiftsFormMap);
			changeShiftsFormMap.clear();
		}
		return "success";
	}
}
